<?php
/**
 * Pertyler DatabaseManagement
 * DatabaseModel
 * Role Classes
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

class Role
{
	private $id;

	private $name;

	private $defaultLanguage;

	public function __Construct($id,
			$name,
			Language $defaultLanguage)
	{
		$this->id = $id;

		$this->name = $name;

		$this->defaultLanguage = $defaultLanguage;
	}

	public function __ToString()
	{
		return $this->name;
	}

	public function DefaultLanguage()
	{
		return $this->defaultLanguage;
	}

	private $privilegeArray;

	private function selectPrivilegeArray()
	{
		if(empty($this->privilegeArray))
		{
			$this->privilegeArray = array();

			$resultArrayArray = Database::Get()->SelectResultArrayArray('
SELECT "RolePrivilege"."Privilege"
	FROM pertyler."Role_privilege" AS "RolePrivilege"
	WHERE "RolePrivilege"."Role" = ' . $this->id);

			foreach($resultArrayArray AS $resultArray)
				$this->privilegeArray[$resultArray['Privilege']] = true;
		}
	}

	public function SelectRolePrivilege(Table_Privilege $privilege)
	{
		if(empty($this->privilegeArray))
			$this->selectPrivilegeArray();

		return isset($this->privilegeArray[$privilege->Id()]);
	}

	public static function Select()
	{
		if(Role_Moderator::Select())
			return Role_Moderator::Select()->Parent();
	}
}

require_once __DIR__ . '/../library/Pert/General/User.php';

class Role_Moderator extends \Pert\User
{
	private $parent;

	private $moderatorId;

	protected function __construct(Role $parent,
			$moderatorId,
			$id)
	{
		$this->parent = $parent;

		$this->moderatorId = $moderatorId;

		parent::__construct($id);
	}

	public static function Get()
	{
		if(parent::Get() instanceof self)
			return parent::Get();
	}

	public static function Select()
	{
		if(self::Get())
			return self::Get();

		if(parent::Get())
		{
			$resultArray = Database::Get()->SelectResultArray('
SELECT "Role".id AS "Role", "Role"."Name" AS "Role_Name", "Role"."DefaultLanguage" AS "Role_DefaultLanguage",
		"Moderator".id,
		"Moderator"."Password"
	FROM pertyler."Role_Moderator" AS "Moderator"
		INNER JOIN pertyler."Role" ON "Moderator"."Role" = "Role".id
	WHERE "Moderator"."Name" = \'' . parent::Get()->Id() . '\'');

			if($resultArray)
			{
				require_once __DIR__ . '/Language.php';

				if($resultArray['Password'] == parent::Get()->password())
					return new self(new Role($resultArray['Role'],
							$resultArray['Role_Name'],
							Language::Select($resultArray['Role_DefaultLanguage'])),
							$resultArray['id'],
							parent::Get()->Id());

				if((md5(trim($resultArray['Password']))) == parent::Get()->password())
				{
					Database::Get()->Update('
UPDATE pertyler."Role_Moderator"
	SET "Password" = \'' . parent::Get()->password() . '\'
	WHERE "Name" = \'' . parent::Get()->Id() . '\'');

					return new self(new Role($resultArray['Role'],
							$resultArray['Role_Name'],
							Language::Select($resultArray['Role_DefaultLanguage'])),
							$resultArray['id'],
							parent::Get()->Id());
				}
			}
		}
	}

	public function Parent()
	{
		return $this->parent;
	}

	public function InsertChangeLog(Table_Privilege $privilege,
			$row)
	{
		Database::Get()->Update('
INSERT INTO pertyler."Moderator_ChangeLog" ("Moderator",
		"Privilege",
		"Ip",
		"Row")
	VALUES (' . $this->moderatorId . ',
			' . $privilege->Id() . ',
			\'' . $this->SelectIP() . '\',
			' . $row . ')');
	}

	public function UpdatePassword($oldPassword,
			$password)
	{
		if(!$this->passwordAvailability($password))
			return false;

		if(md5($oldPassword) != $this->password())
			return false;

		Database::Get()->Update('
UPDATE pertyler."Role_Moderator"
	SET "Password" = \'' . md5($password) . '\'
	WHERE "Name" = \'' . $this->Id() . '\'
			AND "Password" = \'' . md5($oldPassword) . '\'');

		return parent::Login($this,
				$password);
	}
}

class Role_exception extends Database_exception
{
}

class RolePrivilege_exception extends Role_exception
{
	private $privilege;

	public function __construct(Table_Privilege $privilege)
	{
		parent::__construct('Moderator do not have '  . $privilege . ' privilege.');

		$this->privilege = $privilege;
	}
}
